<template>
  <div class="alarm-contant">
    <div class="alarm-card" v-for="device in alarmDevices" :key="device.prop">
      <div class="alarm-title">
        <div class="alarm-title-util">
          <el-image src="/img/icon.png"></el-image>
        </div>
        <div class="alarm-title-name">{{ device.titleEn }}</div>
      </div>
      <div class="alarm-list">
        <div
          class="alarm-item"
          v-for="item in options[device.prop]"
          :key="item.prop"
        >
          <div class="alarm-item-card">
            <div class="alarm-item-name">{{ item.nameEn }}</div>
            <div class="alarm-item-light">
              <div
                class="alarm-light-item"
                v-for="(m, i) in device.lightNumber"
                :key="i"
                :class="{
                  active:
                    alarmList[device.code][item.prop] &&
                    alarmList[device.code][item.prop][i] === 1,
                }"
              >
                {{ device.lightCode[i] }}
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import MQTT from "@/mixins/mqtt";
import {
  mainOptions,
  generatorOptions,
  sideGeneratorOptions,
  codeDict,
} from "@/assets/js/alarmOptions";
import { queryPage } from "@/api/cabin/deviceAlarm";
let interval = null;

export default {
  mixins: [MQTT],
  data() {
    return {
      alarmDevices: [
        {
          title: "主机报警",
          titleEn: "Main Engine",
          prop: "mainOptions",
          lightNumber: 4,
          code: "M",
          lightCode: [1, 2, 3, 4],
        },
        {
          title: "发电机报警",
          titleEn: "Generator",
          prop: "generatorOptions",
          lightNumber: 2,
          code: "G",
          lightCode: ["P", "S"],
        },
        {
          title: "侧推发电机报警",
          titleEn: "Bow Thruster Generator",
          prop: "sideGeneratorOptions",
          lightNumber: 1,
          code: "D",
          lightCode: [""],
        },
      ],
      alarmSort: {
        M: 0,
        G: 1,
        D: 2,
      },
      alarmList: {
        M: {},
        G: {},
        D: {},
      },
      options: {
        mainOptions,
        generatorOptions,
        sideGeneratorOptions,
      },
      codeDict,
    };
  },
  computed: {
    ...mapGetters(["currentMMSI"]),
    topicList() {
      return this.getTopic(["alarm"]);
    },
  },
  methods: {
    getAlarmData(row) {
      this.getAlarm(row.alarmDataList, 1);
      this.getAlarm(row.clearedDataList, 0);
    },
    getAlarm(list, type) {
      for (let item of list) {
        const code = item.deviceNum.replace(/\d/g, ""); // 主机
        const number = item.deviceNum.replace(/\D/g, ""); // 第几盏灯
        const codeProp = codeDict[code][item.alarmCode]; // 报警类型
        this.$set(this.alarmList[code][codeProp], number - 1, type);
      }
    },
    getMainAlarm(row, topic) {
      this.refreshAlarm(row, topic, "M");
    },
    getDGAlarm(row, topic) {
      this.refreshAlarm(row, topic, "G");
    },
    getTGAlarm(row, topic) {
      this.refreshAlarm(row, topic, "D");
    },
    refreshAlarm(row, topic, type) {
      const index = topic.split("/").pop() - 1;
      for (let key in row) {
        if (this.alarmList[type][key]) {
          this.$set(this.alarmList[type][key], index, row[key]);
        }
      }
    },
    resetData() {
      let alarmList = {};
      for (let item of this.alarmDevices) {
        const options = this.options[item.prop];
        alarmList[item.code] = {};
        for (let m of options) {
          alarmList[item.code][m.prop] = Array(item.lightNumber).fill(0);
        }
      }
      this.alarmList = alarmList;
    },
    queryPage() {
      queryPage({
        mmsi: this.currentMMSI,
        pageNum: 1,
        pageSize: 100,
        alarmStatus: 0,
      }).then(({ data }) => {
        for (let item of data.list || []) {
          const code = item.deviceNum.replace(/\d/g, ""); // 主机
          const number = item.deviceNum.replace(/\D/g, ""); // 第几盏灯
          const codeProp = codeDict[code][item.alarmCode]; // 报警类型
          this.$set(this.alarmList[code][codeProp], number - 1, 1);
        }
      });
    },
    initWeb(oldMMSI = 0) {
      if (interval) return;
      interval = 1;
      this.resetData();
      if (oldMMSI != 0) {
        const oldTopicList = this.getTopic(["alarm"], oldMMSI);
        this.mqttUnsubscribe(oldTopicList);
      }
      this.mqttSubscribe(this.topicList);
      this.queryPage();
    },
  },
  created() {
    this.resetData();
  },
  mounted() {
    this.$store
      .dispatch("getShipList")
      .then(() => {
        this.initWeb();
      })
      .catch(() => {});
  },
  destroyed() {
    interval = null;
  },
  watch: {
    currentMMSI(val, oldVal) {
      console.log("当前MMSI: ", val);
      interval = null;
      this.initWeb(oldVal);
    },
  },
};
</script>

<style scoped lang="scss">
.alarm-contant {
  width: 100%;
  height: 100%;
  padding: 15px;
  background-color: #3d3c3c;
  display: grid;
  grid-template-rows: calc(40% - 30px) 30% 30%;
  grid-row-gap: 15px;
  .alarm-card {
    background-color: #504f4f;
    border-radius: 5px;
    box-shadow: 4px 4px 10px #2e2d2d;
    .alarm-title {
      height: 50px;
      line-height: 30px;
      color: #fff;
      display: flex;
      align-items: center;
      border-bottom: 1px solid #888;
      padding: 10px 15px 10px;
      .alarm-title-util {
        width: 12px;
        height: 12px;
        display: flex;
        margin: 6px;
      }
      .alarm-title-name {
        flex: 1;
        margin-left: 10px;
        font-size: 16px;
        color: #00fffc;
      }
    }
    .alarm-list {
      height: calc(100% - 40px);
      display: flex;
      flex-wrap: wrap;
      padding: 5px 15px 15px;
      .alarm-item {
        cursor: pointer;
        width: 20%;
        .alarm-item-card {
          width: calc(100% - 20px);
          border-radius: 5px;
          height: 100%;
          color: #eee;
          text-shadow: 1px 1px 2px #000;
          padding: 0 10px;
          display: flex;
          justify-content: space-between;
          align-items: center;
          .alarm-item-light {
            display: flex;
            .alarm-light-item {
              text-align: center;
              border-radius: 50%;
              color: #ddd;
              text-shadow: 1px 1px 2px #000;
              margin-left: 5px;
              background: radial-gradient(circle at 30% 20%, #a0a0a0, #505050);
              box-shadow: inset 0 4px 8px rgba(255, 255, 255, 0.3),
                0 0 8px rgba(0, 0, 0, 0.5);
              transition: all 0.3s ease-in-out;
            }
            .alarm-light-item.active {
              background: radial-gradient(circle at 30% 20%, #ff4444, #990000);
              box-shadow: 0 0 20px rgba(255, 0, 0, 0.9),
                inset 0 4px 8px rgba(255, 255, 255, 0.3);
              animation: alarm-blink 0.8s infinite alternate;
              color: #fff;
              text-shadow: 1px 1px 2px #000;
            }
          }
        }
        &:nth-of-type(10n + 1),
        &:nth-of-type(10n + 2),
        &:nth-of-type(10n + 3),
        &:nth-of-type(10n + 4),
        &:nth-of-type(10n + 5) {
          .alarm-item-card {
            background-color: #605f5f;
          }
        }
      }
    }
  }
}
@media screen and (max-width: 2000px) {
  .alarm-contant {
    .alarm-item {
      height: 54px;
      font-size: 14px;
      .alarm-light-item {
        width: 20px;
        height: 20px;
        line-height: 20px;
        font-size: 12px;
      }
    }
  }
}
@media screen and (min-width: 2000px) {
  .alarm-contant {
    .alarm-item {
      font-size: 16px;
      height: 70px;
      .alarm-light-item {
        width: 26px;
        height: 26px;
        line-height: 26px;
        font-size: 16px;
      }
    }
  }
}
</style>
